package com.hnx.admin.web.exception;

import com.hnx.admin.json.ResultJson;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.AuthenticationException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/*
项目名称： core
创 建 人: 倔强的头发
创建日期： 2025/5/26   19:38:34 
描   述：
*/
@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {
    @ExceptionHandler(MethodArgumentNotValidException.class)
    public ResultJson MethodArgumentNotValidException(MethodArgumentNotValidException ex, HttpServletRequest request){
        String requestURI = request.getRequestURI();
        String defaultMessage = ex.getBindingResult().getAllErrors().get(0).getDefaultMessage();
        log.error("请求地址'{}',方法参数校验异常'{}'", requestURI, defaultMessage);
        return ResultJson.fail(501,defaultMessage);
    }
    @ExceptionHandler(AuthenticationException.class)
    public ResultJson MethodException(AuthenticationException ex, HttpServletRequest request){
        String requestURI = request.getRequestURI();
        String defaultMessage = ex.getMessage();
        log.error("请求地址'{}',系统验证异常'{}'", requestURI, defaultMessage);
        return ResultJson.fail(502,defaultMessage);
    }
    @ExceptionHandler(Exception.class)
    public ResultJson MethodException(Exception ex, HttpServletRequest request){
        String requestURI = request.getRequestURI();
        String defaultMessage = ex.getMessage();
        log.error("请求地址'{}',系统异常'{}'", requestURI, defaultMessage);
        return ResultJson.fail(505,defaultMessage);
    }
}
